package com.scott.controller;

import com.scott.service.GeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Scott on 2017/4/27.
 */
@Controller
public class DownLoadCodeContorller {
    @Autowired
    GeneratorService generatorService;
    @RequestMapping("/generatorCode")
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String dbName = request.getParameter("dbName");
        String tableName = request.getParameter("tableName");

        byte[] data = generatorService.generatorCode(tableName, dbName);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
